<div class="container">

        <h1 id="pkg-overview">package scanner</h1>

        <p><code>import "go/scanner"</code>

        </p><p>
Package scanner implements a scanner for Go source text.
It takes a []byte as source which can then be tokenized
through repeated calls to the Scan method.</p>


        
  


        
        

        

        

        
        
          
          
        

        
        


<p><a href="https://github.com/golang/go/blob/master/src/go/scanner/errors.go">errors.go</a> <a href="https://github.com/golang/go/blob/master/src/go/scanner/scanner.go">scanner.go</a> </p>

        
        

        
        

        
        
        
          <h2 data-kind="f" id="PrintError">func <a href="https://github.com/golang/go/blob/master/src/go/scanner/errors.go#L116" title="View Source">PrintError</a>  </h2>
          <p>
PrintError is a utility function that prints a list of errors to w,
one error per line, if the err parameter is an ErrorList. Otherwise
it prints the err string.
</p>

          
  

        

        
        

        
          <h2 data-kind="t" id="Error">type <a href="https://github.com/golang/go/blob/master/src/go/scanner/errors.go#L19" title="View Source">Error</a>  </h2>
          <p>
In an ErrorList, an error is represented by an *Error.
The position Pos, if valid, points to the beginning of
the offending token, and the error condition is described
by Msg.
</p>

          
          
          
  


          

          
            <h3 data-kind="m" id="Error.Error">func (Error) <a href="https://github.com/golang/go/blob/master/src/go/scanner/errors.go#L25" title="View Source">Error</a>  </h3>
            <p>
Error implements the error interface.
</p>

            
  

          
        
          <h2 data-kind="t" id="ErrorHandler">type <a href="https://github.com/golang/go/blob/master/src/go/scanner/scanner.go#L26" title="View Source">ErrorHandler</a>  </h2>
          <p>
An ErrorHandler may be provided to Scanner.Init. If a syntax error is
encountered and a handler was installed, the handler is called with a
position and an error message. The position points to the beginning of
the offending token.
</p>

          
          
          
  


          

          
        
          <h2 data-kind="t" id="ErrorList">type <a href="https://github.com/golang/go/blob/master/src/go/scanner/errors.go#L37" title="View Source">ErrorList</a>  </h2>
          <p>
ErrorList is a list of *Errors.
The zero value for an ErrorList is an empty ErrorList ready to use.
</p>

          
          
          
  


          

          
            <h3 data-kind="m" id="ErrorList.Add">func (*ErrorList) <a href="https://github.com/golang/go/blob/master/src/go/scanner/errors.go#L40" title="View Source">Add</a>  </h3>
            <p>
Add adds an Error with given position and error message to an ErrorList.
</p>

            
  

          
            <h3 data-kind="m" id="ErrorList.Err">func (ErrorList) <a href="https://github.com/golang/go/blob/master/src/go/scanner/errors.go#L105" title="View Source">Err</a>  </h3>
            <p>
Err returns an error equivalent to this error list.
If the list is empty, Err returns nil.
</p>

            
  

          
            <h3 data-kind="m" id="ErrorList.Error">func (ErrorList) <a href="https://github.com/golang/go/blob/master/src/go/scanner/errors.go#L93" title="View Source">Error</a>  </h3>
            <p>
An ErrorList implements the error interface.
</p>

            
  

          
            <h3 data-kind="m" id="ErrorList.Len">func (ErrorList) <a href="https://github.com/golang/go/blob/master/src/go/scanner/errors.go#L48" title="View Source">Len</a>  </h3>
            <p>
ErrorList implements the sort Interface.
</p>

            
  

          
            <h3 data-kind="m" id="ErrorList.Less">func (ErrorList) <a href="https://github.com/golang/go/blob/master/src/go/scanner/errors.go#L51" title="View Source">Less</a>  </h3>
            
            
  

          
            <h3 data-kind="m" id="ErrorList.RemoveMultiples">func (*ErrorList) <a href="https://github.com/golang/go/blob/master/src/go/scanner/errors.go#L78" title="View Source">RemoveMultiples</a>  </h3>
            <p>
RemoveMultiples sorts an ErrorList and removes all but the first error per line.
</p>

            
  

          
            <h3 data-kind="m" id="ErrorList.Reset">func (*ErrorList) <a href="https://github.com/golang/go/blob/master/src/go/scanner/errors.go#L45" title="View Source">Reset</a>  </h3>
            <p>
Reset resets an ErrorList to no errors.
</p>

            
  

          
            <h3 data-kind="m" id="ErrorList.Sort">func (ErrorList) <a href="https://github.com/golang/go/blob/master/src/go/scanner/errors.go#L73" title="View Source">Sort</a>  </h3>
            <p>
Sort sorts an ErrorList. *Error entries are sorted by position,
other errors are sorted by error message, and before any *Error
entry.
</p>

            
  

          
            <h3 data-kind="m" id="ErrorList.Swap">func (ErrorList) <a href="https://github.com/golang/go/blob/master/src/go/scanner/errors.go#L49" title="View Source">Swap</a>  </h3>
            
            
  

          
        
          <h2 data-kind="t" id="Mode">type <a href="https://github.com/golang/go/blob/master/src/go/scanner/scanner.go#L91" title="View Source">Mode</a>  </h2>
          <p>
A mode value is a set of flags (or 0).
They control scanner behavior.
</p>

          
          
          
  


          

          
        
          <h2 data-kind="t" id="Scanner">type <a href="https://github.com/golang/go/blob/master/src/go/scanner/scanner.go#L32" title="View Source">Scanner</a>  </h2>
          <p>
A Scanner holds the scanner's internal state while processing
a given text. It can be allocated as part of another data
structure but must be initialized via Init before use.
</p>

          
          
          
  


          

          
            <h3 data-kind="m" id="Scanner.Init">func (*Scanner) <a href="https://github.com/golang/go/blob/master/src/go/scanner/scanner.go#L113" title="View Source">Init</a>  </h3>
            <p>
Init prepares the scanner s to tokenize the text src by setting the
scanner at the beginning of src. The scanner uses the file set file
for position information and it adds line information for each line.
It is ok to re-use the same file when re-scanning the same file as
line information which is already present is ignored. Init causes a
panic if the file size does not match the src size.
</p>
<p>
Calls to Scan will invoke the error handler err if they encounter a
syntax error and err is not nil. Also, for each error encountered,
the Scanner field ErrorCount is incremented by one. The mode parameter
determines how comments are handled.
</p>
<p>
Note that Init may call err if there is an error in the first character
of the file.
</p>

            
  

          
            <h3 data-kind="m" id="Scanner.Scan">func (*Scanner) <a href="https://github.com/golang/go/blob/master/src/go/scanner/scanner.go#L602" title="View Source">Scan</a>  </h3>
            <p>
Scan scans the next token and returns the token position, the token,
and its literal string if applicable. The source end is indicated by
token.EOF.
</p>
<p>
If the returned token is a literal (token.IDENT, token.INT, token.FLOAT,
token.IMAG, token.CHAR, token.STRING) or token.COMMENT, the literal string
has the corresponding value.
</p>
<p>
If the returned token is a keyword, the literal string is the keyword.
</p>
<p>
If the returned token is token.SEMICOLON, the corresponding
literal string is ";" if the semicolon was present in the source,
and "\n" if the semicolon was inserted because of a newline or
at EOF.
</p>
<p>
If the returned token is token.ILLEGAL, the literal string is the
offending character.
</p>
<p>
In all other cases, Scan returns an empty literal string.
</p>
<p>
For more tolerant parsing, Scan will return a valid token if
possible even if a syntax error was encountered. Thus, even
if the resulting token sequence contains no illegal tokens,
a client may not assume that no error occurred. Instead it
must check the scanner's ErrorCount or the number of calls
of the error handler, if there was one installed.
</p>
<p>
Scan adds line information to the file added to the file
set with Init. Token positions are relative to that file
and thus relative to the file set.
</p>

            
  
    <div class="panel-group">
    
      <div class="panel panel-default" id="example-Scanner-Scan">
        <div class="panel-heading"><a class="accordion-toggle" data-toggle="collapse" href="#ex-Scanner-Scan">Example</a></div>
        <div class="panel-collapse collapse" id="ex-Scanner-Scan"><div class="panel-body">
          
          <p>Code:<span class="pull-right"><a href="?play=Scanner-Scan">play</a> </span>
          </p><pre><span class="com">// src is the input that we want to tokenize.</span>
src := []byte("cos(x) + 1i*sin(x) // Euler")

<span class="com">// Initialize the scanner.</span>
var s scanner.Scanner
fset := token.NewFileSet()                      <span class="com">// positions are relative to fset</span>
file := fset.AddFile("", fset.Base(), len(src)) <span class="com">// register input "file"</span>
s.Init(file, src, nil <span class="com">/* no error handler */</span>, scanner.ScanComments)

<span class="com">// Repeated calls to Scan yield the token sequence found in the input.</span>
for {
    pos, tok, lit := s.Scan()
    if tok == token.EOF {
        break
    }
    fmt.Printf("%s\t%s\t%q\n", fset.Position(pos), tok, lit)
}</pre>
          <p>Output:</p><pre>1:1	IDENT	"cos"
1:4	(	""
1:5	IDENT	"x"
1:6	)	""
1:8	+	""
1:10	IMAG	"1i"
1:12	*	""
1:13	IDENT	"sin"
1:16	(	""
1:17	IDENT	"x"
1:18	)	""
1:20	;	"\n"
1:20	COMMENT	"// Euler"
</pre>
        </div></div>
      </div>
    
    </div>
  

<div class="clearfix" id="x-footer">
  <div class="container">
    <a href="http://studygolang.com/" target="_blank">Go语言中文网</a>
    <span class="text-muted">|</span> <a href="http://golang.org/" target="_blank">Go Language</a>
    <span class="pull-right"><a href="#">Back to top</a></span>
  </div>
</div>
<script src="../assets/jquery-2.0.3.min.js"></script>
<script src="../assets/bootstrap.min.js"></script>
<script src="../assets/site.js"></script>


</div>